<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>申请详情 - 售后管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f5f5;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            font-size: 24px;
        }

        .back-btn {
            background: rgba(255, 255, 255, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            padding: 8px 16px;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
            padding: 20px;
        }

        .detail-card {
            background: white;
            border-radius: 10px;
            padding: 30px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .detail-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid #e1e5e9;
        }

        .application-id {
            font-size: 24px;
            font-weight: bold;
            color: #333;
        }

        .status-badge {
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 14px;
            font-weight: 500;
        }

        .status-pending {
            background-color: #fff3cd;
            color: #856404;
        }

        .status-processing {
            background-color: #cce5ff;
            color: #004085;
        }

        .status-completed {
            background-color: #d4edda;
            color: #155724;
        }

        .status-rejected {
            background-color: #f8d7da;
            color: #721c24;
        }

        .detail-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 30px;
            margin-bottom: 30px;
        }

        .detail-section {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
        }

        .section-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin-bottom: 15px;
        }

        .detail-item {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
            padding: 8px 0;
            border-bottom: 1px solid #e1e5e9;
        }

        .detail-label {
            font-weight: 500;
            color: #666;
        }

        .detail-value {
            color: #333;
            text-align: right;
        }

        .voucher-section {
            margin-top: 20px;
        }

        .voucher-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 15px;
            margin-top: 15px;
        }

        .voucher-item {
            border: 1px solid #e1e5e9;
            border-radius: 8px;
            padding: 15px;
            text-align: center;
        }

        .voucher-item img {
            max-width: 100%;
            height: auto;
            border-radius: 5px;
            margin-bottom: 10px;
        }

        .voucher-item a {
            color: #667eea;
            text-decoration: none;
        }

        .voucher-item a:hover {
            text-decoration: underline;
        }

        .action-buttons {
            display: flex;
            gap: 15px;
            margin-top: 20px;
            flex-wrap: wrap;
        }

        .action-btn {
            padding: 10px 20px;
            border: 1px solid #667eea;
            background: white;
            color: #667eea;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.2s ease;
        }

        .action-btn:hover {
            background: #667eea;
            color: white;
        }

        .action-btn.primary {
            background: #667eea;
            color: white;
        }

        .action-btn.primary:hover {
            background: #5a6fd8;
        }

        .loading {
            text-align: center;
            padding: 40px;
        }

        .spinner {
            border: 2px solid #f3f3f3;
            border-top: 2px solid #667eea;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            display: inline-block;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .error-message {
            color: #e74c3c;
            text-align: center;
            padding: 20px;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>申请详情</h1>
        <a href="/after_sale_system/page/consumer/applicationList" class="back-btn">返回列表</a>
    </div>

    <div class="container">
        <div id="detailContainer">
            <div class="loading">
                <div class="spinner"></div>
                <p>加载中...</p>
            </div>
        </div>
    </div>

    <script>
        // 获取URL参数
        function getUrlParameter(name) {
            name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
            var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
            var results = regex.exec(location.search);
            return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
        }

        // 页面加载时获取申请详情
        window.addEventListener('load', function() {
            const applicationId = getUrlParameter('id');
            if (applicationId) {
                loadApplicationDetail(applicationId);
            } else {
                showError('缺少申请ID参数');
            }
        });

        // 加载申请详情
        function loadApplicationDetail(id) {
            fetch(`/after_sale_system/api/afterSale/detail/${id}`)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        displayApplicationDetail(data.data);
                    } else {
                        showError('获取申请详情失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('获取申请详情失败:', error);
                    showError('获取申请详情失败，请稍后重试');
                });
        }

        // 显示申请详情
        function displayApplicationDetail(application) {
            const container = document.getElementById('detailContainer');
            
            container.innerHTML = `
                <div class="detail-card">
                    <div class="detail-header">
                        <div class="application-id">申请编号: ${application.id}</div>
                        <div class="status-badge status-${getStatusClass(application.status)}">
                            ${getStatusText(application.status)}
                        </div>
                    </div>

                    <div class="detail-grid">
                        <div class="detail-section">
                            <div class="section-title">基本信息</div>
                            <div class="detail-item">
                                <span class="detail-label">申请类型</span>
                                <span class="detail-value">${getTypeText(application.afterSaleType)}</span>
                            </div>
                            <div class="detail-item">
                                <span class="detail-label">申请时间</span>
                                <span class="detail-value">${formatDate(application.applyTime)}</span>
                            </div>
                            <div class="detail-item">
                                <span class="detail-label">订单号</span>
                                <span class="detail-value">${application.orderId || 'N/A'}</span>
                            </div>
                            <div class="detail-item">
                                <span class="detail-label">商品名称</span>
                                <span class="detail-value">${application.productName || 'N/A'}</span>
                            </div>
                        </div>

                        <div class="detail-section">
                            <div class="section-title">处理信息</div>
                            <div class="detail-item">
                                <span class="detail-label">审核时间</span>
                                <span class="detail-value">${formatDate(application.auditTime) || 'N/A'}</span>
                            </div>
                            <div class="detail-item">
                                <span class="detail-label">处理时间</span>
                                <span class="detail-value">${formatDate(application.handleTime) || 'N/A'}</span>
                            </div>
                            <div class="detail-item">
                                <span class="detail-label">处理备注</span>
                                <span class="detail-value">${application.handleRemark || 'N/A'}</span>
                            </div>
                            ${application.rejectReason ? `
                                <div class="detail-item">
                                    <span class="detail-label">拒绝原因</span>
                                    <span class="detail-value">${application.rejectReason}</span>
                                </div>
                            ` : ''}
                        </div>
                    </div>

                    ${application.voucherUrls ? `
                        <div class="voucher-section">
                            <div class="section-title">上传凭证</div>
                            <div class="voucher-list">
                                ${displayVouchers(application.voucherUrls)}
                            </div>
                        </div>
                    ` : ''}

                    <div class="action-buttons">
                        <button class="action-btn" onclick="viewCommunication(${application.id})">查看沟通记录</button>
                        ${application.status === 'PENDING_AUDIT' ? 
                            `<button class="action-btn" onclick="cancelApplication(${application.id})">取消申请</button>` : ''
                        }
                        ${application.status === 'COMPLETED' ? 
                            `<button class="action-btn primary" onclick="submitFeedback(${application.id})">提交反馈</button>` : ''
                        }
                    </div>
                </div>
            `;
        }

        // 显示凭证
        function displayVouchers(voucherUrls) {
            if (!voucherUrls) return '';
            
            const urls = voucherUrls.split(',').filter(url => url.trim());
            return urls.map(url => {
                const fileName = url.split('/').pop();
                const isImage = /\.(jpg|jpeg|png|gif|webp)$/i.test(fileName);
                
                if (isImage) {
                    return `
                        <div class="voucher-item">
                            <img src="${url}" alt="凭证" onclick="openImage('${url}')" style="cursor: pointer;">
                            <div><a href="${url}" target="_blank">${fileName}</a></div>
                        </div>
                    `;
                } else {
                    return `
                        <div class="voucher-item">
                            <div style="font-size: 48px; color: #667eea; margin-bottom: 10px;">📄</div>
                            <div><a href="${url}" target="_blank">${fileName}</a></div>
                        </div>
                    `;
                }
            }).join('');
        }

        // 打开图片
        function openImage(url) {
            window.open("/after_sale_system"+url, '_blank');
        }

        // 获取状态样式类
        function getStatusClass(status) {
            const statusMap = {
                'PENDING_AUDIT': 'pending',
                'PROCESSING': 'processing',
                'COMPLETED': 'completed',
                'REJECTED': 'rejected'
            };
            return statusMap[status] || 'pending';
        }

        // 获取状态文本
        function getStatusText(status) {
            const statusMap = {
                'PENDING_AUDIT': '待审核',
                'PROCESSING': '处理中',
                'COMPLETED': '已完成',
                'REJECTED': '已拒绝'
            };
            return statusMap[status] || status;
        }

        // 获取类型文本
        function getTypeText(type) {
            const typeMap = {
                'RETURN': '退货',
                'EXCHANGE': '换货',
                'REPAIR': '维修'
            };
            return typeMap[type] || type;
        }

        // 格式化日期
        function formatDate(dateString) {
            if (!dateString) return 'N/A';
            const date = new Date(dateString);
            return date.toLocaleString('zh-CN');
        }

        // 查看沟通记录
        function viewCommunication(id) {
            window.location.href = `/after_sale_system/page/consumer/communication?id=${id}`;
        }

        // 取消申请
        function cancelApplication(id) {
            if (confirm('确定要取消这个申请吗？')) {
                fetch(`/after_sale_system/api/afterSale/cancel/${id}`, {
                    method: 'DELETE'
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('申请已取消');
                        window.location.href = '/after_sale_system/page/consumer/applicationList';
                    } else {
                        alert('取消失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('取消申请失败:', error);
                    alert('取消申请失败，请稍后重试');
                });
            }
        }

        // 提交反馈
        function submitFeedback(id) {
            window.location.href = `/after_sale_system/page/consumer/feedback?id=${id}`;
        }

        function showError(message) {
            document.getElementById('detailContainer').innerHTML = `
                <div class="error-message">${message}</div>
            `;
        }
    </script>
</body>
</html> 